<template>
  <!-- 根组件 -->
  <div class="pageRoot">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="7">
          <el-input placeholder="请输入内容" clearable v-model="userParams.query" @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showDialog('adduser')">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="userList" border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="username" label="姓名" width="100"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="170"></el-table-column>
        <el-table-column prop="mobile" label="电话" width="130"></el-table-column>
        <el-table-column prop="mg_state" label="登录权限" width="80">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state" @change="changeStatus(scope.row)" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="role_name" label="角色" width="100"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" width="450px">
            <el-button size="small" type="primary" icon="el-icon-edit" @click="showDialog('editUser',scope.row)">编辑</el-button>

            <el-button size="small" type="danger" icon="el-icon-delete" @click="showDialog('delUser',scope.row.id)">删除</el-button>

            <el-button size="small" type="warning" icon="el-icon-s-tools" @click="showDialog('assignRole',scope.row)">分配角色</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="userParams.pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="userParams.pagesize" layout="sizes, prev, pager, next" :total="totalUser"></el-pagination>
    </el-card>

    <add-user ref="adduser"></add-user>
    <edit-user ref="editUser"></edit-user>
    <del-user ref="delUser"></del-user>
    <assign-role ref="assignRole"></assign-role>
  </div>
</template>

<script>
import addUser from '../../dialog/addUser'
import editUser from '../../dialog/editUser'
import delUser from '../../dialog/delUser'
import assignRole from '../../dialog/assignRole'
export default {
  name: '',
  components: { addUser, editUser, delUser, assignRole },
  data () {
    return {
      dialogShow: false,
      userList: [],
      totalUser: 0,
      userParams: {
        query: '',
        pagenum: 1, // 当前显示的页码
        pagesize: 10
      }
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    // 显示弹窗
    showDialog (...val) {
      this.dialogShow = true
      this.$refs[val[0]].openDialog([this.dialogShow, val[1]])
    },
    // 获取用户列表
    async getUserList () {
      const { data: res } = await this.axios.get('users', {
        params: this.userParams
      })
      if (res.meta.status !== 200) { return this.$message.error('获取用户列表失败') }
      this.userList = res.data.users

      this.totalUser = res.data.total
    },
    // 更改用户状态
    async changeStatus (userInfo) {
      const { data: res } = await this.axios.put(
        'users/' + userInfo.id + '/state/' + userInfo.mg_state
      )
      if (res.meta.status !== 200) { return this.$message.error('更新用户状态失败') }
      this.$message.success('更新用户状态成功')
    },
    // 每页显示条数设置
    handleSizeChange (newSize) {
      this.userParams.pagesize = newSize
      this.getUserList()
    },
    // 页码改变事件
    handleCurrentChange (newSize) {
      this.userParams.pagenum = newSize
      this.getUserList()
    }
  }
}
</script>

<style scoped >
</style>
